package com.ls.fw.data.statement;

import java.util.ArrayList;
import java.util.List;

import com.ls.fw.data.bean.From;
import com.ls.fw.data.bean.FromItem;
import com.ls.fw.data.bean.Table;
import com.ls.fw.data.enums.JoinType;
import com.ls.fw.data.exp.Compound;
import com.ls.fw.data.exp.Where;

/**
 * where条件
 * @author ls
 * 2014年11月21日下午3:16:08
 */
public abstract class BaseWhere extends Operate {

	private Where where = null;
	
	protected From from = new From();
	private List<String> ids = new ArrayList<String>();
	
	/**
	 * 添加一个需要删除的索引ID
	 * @author ls
	 * 2014年11月22日下午4:28:33 
	 * @param id
	 */
	public void addId(String id){
		this.ids.add(id);
	}
	
	
	/**
	 * 是否存在需要删除索引ID
	 * @author ls
	 * 2014年11月22日下午4:28:47 
	 * @return
	 */
	public boolean hasId(){
		return !this.ids.isEmpty();
	}
	
	public List<String> getIds() {
		return ids;
	}

	public void setIds(List<String> ids) {
		this.ids = ids;
	}
 
	public boolean hasWhere(){
		return this.where!=null && this.where.hasCondition();
	}
	
	public Where getWhere() {
		return this.where;
	}

	public void setWhere(Where where) {
		this.where = where;
	}

	public void setWhere(Compound where) {
		this.where = new Where(where);
	}

	@Override
	public String toString() {
		return "BaseWhere [where=" + where + "]";
	}


	/**
	 * @return the from
	 */
	public From getFrom() {
		return from;
	}


	/**
	 * @param from the from to set
	 */
	public void setFrom(From from) {
		this.from = from;
	}
	
	/**
	 * @param table the table to set
	 */
	public void addFromItem(Table table) {
		from.addFromItem(table);
	}
	
	public void addFromItem(String schemaName, String name) {
		from.addFromItem(schemaName, name);
	}
	
	public void addFromItem(String name) {
		from.addFromItem(name);
	}

	public void setTable(String name) {
		from.addFromItem(name);
	}
	public void setTable(Table table) {
		from.addFromItem(table);
	}
	public void setFromItem(Table table) {
		from.addFromItem(table);
	}
	
	public From addFromItem(String table1, JoinType joinType, String table2,
			String table1Field, String table2Field){
		return this.from.addFromItem(table1, joinType, table2, table1Field, table2Field);
	}
	
	public From addFromItem(FromItem fromItem){
		return this.from.addFromItem(fromItem);
	}
	

	public Table getTable(){
		return this.from.getTables().size()>0?this.from.getTables().get(0):null;
	}
}
